<template>
  <BaseForm v-model="formDataModel" @close="$emit('callBack')" />

</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import AssistProofingsInfoApi from '/@/api/webPath/AssistProofingsInfoApi.ts';
import { translate } from "/@/i18n";
import FORM_FIELD_TYPE from "/@/constants/FormFieldType.ts";
import { useSysDataList } from '~/src/utils/useSysDataList';
import { useUserName } from '~/src/utils/useUserName';
import { useSysDataPidList } from '~/src/utils/useSysDataPid';
defineOptions({
  name: 'AssistProofingsInfoEdit',
})
const props = defineProps({
  modelValue: {

  }
})
const emit = defineEmits(['fetch-data', 'callBack'])

const formRef = ref<FormInstance>();
const title = ref<string>('');
const dialogFormVisible = ref<boolean>(false);

// 时间  日期
// 创建一个 Date 对象，表示当前时间
const now = new Date();
// 获取年份
const year = now.getFullYear(); // 获取完整的年份（例如：2023）
// 获取月份
const month = now.getMonth() + 1; // 获取月份（0-11，0 表示 1 月，所以需要 + 1）
// 获取日期
const day = now.getDate(); // 获取日期（1-31）
// 获取小时
const hours = now.getHours(); // 获取小时（0-23）
// 获取分钟
const minutes = now.getMinutes(); // 获取分钟（0-59）
// 获取秒数
const seconds = now.getSeconds(); // 获取秒数（0-59）
// 格式化年月日
const yearMonthDay = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
// 格式化时分秒
const hoursMinutesSeconds = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
// 当前的时分秒
const dateTime = `${yearMonthDay} ${hoursMinutesSeconds}`;
const defaultDateRange = ref<string[]>([]);

const userName = useUserName()
const { value: areaNameList } = useSysDataList("region_name_type")
const { value: typeList, pid: pidList } = useSysDataList("three_proof_equipment_type")
const deviceNameList = ref([])
const formDataModel = ref({
  columnCount: 1,
  expandLimit: 6,
  autoFit: false,
  columnWidth: '300px',
  labelPosition: 'right',
  labelWidth: '100px',
  modelData: {},  //表单参数值 初始化
  callback: async (any: object) => {  //表单执行成功后回调
    console.log("any", any)
    const obj = {
      assistProofingsInfo: {
        checkDate: `${any.checkDate} 00:00:00`,
        operator: any.operator,
      },
      list: any.list
    }
    console.log("any data", any)
    if (props.modelValue.query && props.modelValue.query.assistProofingsInfoId) {
      Object.assign(obj.assistProofingsInfo, { id: props.modelValue.query.assistProofingsInfoId })
    }


    console.log("final obj111111", obj)
    //完成数据提交
    const { message } = await AssistProofingsInfoApi.add(obj)
    $baseMessage(message, 'success', 'vab-hey-message-success');
    dialogFormVisible.value = false


    //事件及数据推送到上一层父类
    emit('callBack', any); //把表单数据推送到上一层
  },
  items: [  //表单item数据初始化
    {
      label: "检查日期",
      field: "checkDate",
      clearable: true,
      defaultValue: yearMonthDay,
      type: FORM_FIELD_TYPE.DATE,
      placeholder: "请输入",

      config: {
        props: {   //日期精度配置
          type: "daterange",
          startPlaceholder: "开始日期",
          endPlaceholder: "结束日期",
          format: "YYYY-MM-DD",
        }
      },
    },
    // {
    //   label: "区域名称",
    //   field: "areaName",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUT,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    // {
    //   label: "类型",
    //   field: "type",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUT,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    // {
    //   label: "设备名称",
    //   field: "deviceName",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUT,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    // {
    //   label: "设备数量",
    //   field: "deviceNum",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUTNUMBER,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    // {
    //   label: "检查日期",
    //   field: "checkDate",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.DATE,
    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    //   config: {
    //     props: {   //日期精度配置
    //       type: "daterange",
    //       startPlaceholder: "开始日期",
    //       endPlaceholder: "结束日期",
    //       format: "YYYY-MM-DD",
    //     }
    //   },
    // },
    // {
    //   label: "设备状态",
    //   field: "deviceStatus",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUT,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    // {
    //   label: "捕获虫害情况",
    //   field: "captureStatus",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUT,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    // {
    //   label: "检查人",
    //   field: "operator",
    //   clearable: true,
    //   type: FORM_FIELD_TYPE.INPUT,

    //   placeholder: "请输入",

    //   rule: {
    //     trigger: 'blur',
    //     validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //       callback();
    //     },
    //   },

    // },
    {
      field: "list",
      type: FORM_FIELD_TYPE.EDITABLETABLE,
      modelData: {
        data: [],
        columns: [
          {
            field: "id",
            hidden: true,
            width: 200
          },
          {
            label: "区域名称",
            field: "areaName",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: areaNameList,
            width: 200
          },
          {
            label: "类型",
            field: "type",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: typeList,
            async callback(v) {
              console.log("vvvvv", v)
              // deviceNameList.value =  
              console.log("pidList", pidList.value)
              const typeItem = pidList.value.find(x => x.value === v)
              const pid = typeItem && typeItem.pid
              console.log("current pid", pid)
              deviceNameList.value = (await useSysDataPidList("three_proof_equipment_type", pid)).value
              console.log("deviceNameList", deviceNameList.value)
              console.log("type value", v)
            },
            width: 200
          },
          {
            label: "设备名称",
            field: "deviceName",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: deviceNameList,
            width: 200
          },
          {
            label: "设备数量",
            field: "deviceNum",
            width: 200
          },
          {
            label: "设备状态",
            field: "deviceStatus",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: [
              {
                label: "完好",
                value: "1"
              },
              {
                label: "异常",
                value: "2"
              }
            ],
            width: 200
          },
          {
            label: "捕获虫害情况",
            field: "captureStatus",
            type: FORM_FIELD_TYPE.SINGLE_LAYER_SELECT,
            options: [
              {
                label: "无",
                value: "1"
              },
              {
                label: "有",
                value: "2"
              }
            ],
            width: 150
          },
          {
            label: "检查人",
            field: "operator",
            type: FORM_FIELD_TYPE.READONLY,
            defaultValue: userName,
          }
        ]
      }

    }
    //   {
    //     label: "所属公司",
    //     field: "companyId",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "创建人",
    //     field: "creator",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "修改人",
    //     field: "reviser",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "排序",
    //     field: "sort",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUTNUMBER,
    //     defaultValue: 0,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },

  ],
})






//页面数据加载
onMounted(async () => {

  //初始化数据model
  if (props.modelValue.query) { //跳转过来时候有参数
    if (props.modelValue.query.assistProofingsInfoId) {  //id存在则为编辑，加载网络数据，如上级页面带参数手动覆盖

      const { data } = await AssistProofingsInfoApi.getDetail({
        assistProofingsInfoId: props.modelValue.query.assistProofingsInfoId   //从路由中获取参数
      })

      const { data: { records: list } } = await AssistProofingsInfoApi.getDetailPageData({
        check_id: props.modelValue.query.assistProofingsInfoId,

      })
      Object.assign(data, { list })
      formDataModel.value.modelData = data;
      console.log("form datamodle value", formDataModel.value.modelData)
    } else {
      //新增时候则用父类页面传参默认填充可根据业务修改
      for (const key in props.modelValue.query) {
        formDataModel.value.modelData[key] = props.modelValue.query[key]
      }
    }
    // defaultDateRange.value = [yearMonthDay]; // 设置默认的日期范围为今天
    // formDataModel.value.modelData.checkDate = defaultDateRange.value;
  }
})


//方法暴露区域  todo
defineExpose({

})

</script>
